<template>
  <div>
    <el-button type="primary" icon="el-icon-plus" @click="addTM"
      >添加</el-button
    >
    <el-table :data="trademarkInfo.records" style="width: 100%" border>
      <el-table-column type="index" label="序号" width="100px" align="center">
      </el-table-column>
      <el-table-column prop="tmName" label="品牌名称" width="400px">
      </el-table-column>
      <el-table-column prop="logoUrl" label="品牌LOGO" width="400px">
        <!-- 作用域插槽 -->
        <template slot-scope="scope">
          <img
            style="width: 100px"
            :src="scope.row.logoUrl"
            alt="图片路径损坏"
          />
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit"
            @click="editDialog(scope)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="deleteTrademark(scope)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 3, 5, 10]"
      :page-size="pageSize"
      :pager-count="7"
      layout="prev, pager, next, jumper,->, sizes,total"
      :total="trademarkInfo.total"
    >
    </el-pagination>
    <!-- 添加品牌 dialog对话框 -->
    <el-dialog
      :title="isAdd ? '添加品牌' : '修改品牌'"
      :visible.sync="dialogVisible"
      width="50%"
      @close="handleClose"
    >
      <!-- 表单 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="品牌名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" prop="logoUrl">
          <!-- 上传图片 -->
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img
              v-if="ruleForm.logoUrl"
              :src="ruleForm.logoUrl"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTrademark">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
export default {
  name: "Trademark",
  data() {
    return {
      tableData: [],
      // 当前页码
      currentPage: 3,
      // 每页条数
      pageSize: 3,
      // 控制对话框是否显示
      dialogVisible: false,
      ruleForm: {
        name: "",
        logoUrl: "",
      },
      rules: {
        name: [{ required: true, message: "请输入品牌名称", trigger: "blur" }],
        logoUrl: [
          { required: true, message: "请选择品牌图片", trigger: "blur" },
        ],
      },
      // 区分是添加还是编辑品牌的标识
      isAdd: true,
      // 编辑信息id
      editTMInfo: {
        id: "",
      },
    };
  },
  mounted() {
    this.getTrademarkList();
  },
  computed: {
    ...mapGetters(["trademarkInfo"]),
    // ...mapState('produce',['trademarkInfo'])
  },
  methods: {
    getTrademarkList() {
      this.$store.dispatch("trademark/getTrademarkList", {
        page: this.currentPage,
        limit: this.pageSize,
      });
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.getTrademarkList();
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.getTrademarkList();
    },
    // 对话框关闭时
    handleClose() {
      // 清除对话框
      this.$refs.ruleForm.resetFields();
    },
    // logo图片上传成功
    handleAvatarSuccess(res) {
      this.ruleForm.logoUrl = res.data;
    },
    // 点击添加按钮
    addTM() {
      this.isAdd = true;
      this.dialogVisible = true;
    },
    // 确定添加品牌，为了使添加和修改共用一个对话框
    addTrademark() {
      // 表单验证
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          // 如果是添加
          if (this.isAdd) {
            const res = await this.$store.dispatch("trademark/addTrademark", {
              logoUrl: this.ruleForm.logoUrl,
              tmName: this.ruleForm.name,
            });
            this.dialogVisible = false;
            if (res === "ok") this.$message.success("添加品牌成功");
            else this.$message.error("添加品牌失败");
            // 重新获取数据
            this.getTrademarkList();
          } else {
            // 如果是编辑
            const res = await this.$store.dispatch("trademark/editTrademark", {
              id: this.editTMInfo.id,
              logoUrl: this.ruleForm.logoUrl,
              tmName: this.ruleForm.name,
            });
            if(res==='ok') this.$message.success('修改品牌成功')
            else this.$message.error('修改品牌失败')
            this.dialogVisible = false;
            // 重新获取数据
            this.getTrademarkList();
          }
        } else {
          alert("表单验证失败");
        }
      });
    },
    // 编辑品牌
    editDialog({ row }) {
      this.isAdd = false;
      // 修改时的id
      this.editTMInfo.id = row.id;
      // 展示当前修改的品牌信息
      this.ruleForm.name = row.tmName;
      this.ruleForm.logoUrl = row.logoUrl;
      this.dialogVisible = true;
    },
    // 删除品牌
    deleteTrademark({ row }) {
      this.$confirm(`是否要删除${row.tmName}品牌？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await this.$store.dispatch("trademark/deleteTrademark", {id: row.id});
          if (res === "ok") {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // 重新获取数据
            this.getTrademarkList()
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
.el-button {
  margin-bottom: 20px;
}
.el-table {
  margin-bottom: 20px;
}
.el-pagination {
  text-align: center;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>